package com.xxx.system.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.system.annotation.PreAuthorize;
import com.xxx.system.domain.Role;
import com.xxx.system.query.RoleQuery;
import com.xxx.system.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //默认返回json格式，前后端分离
@RequestMapping("/role")
@Api(tags = "角色接口类")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    //所有controller的接口  都是Restful风格
    //查询所有
    //localhost:8080/role  ---Get请求方式
    @GetMapping
    @ApiOperation(value = "查询所有角色")
    @PreAuthorize(name = "查询所有角色", sn = "role:findAll", descs = "查询所有角色")
    public R<List<Role>> findAll() {
        List<Role> list = roleService.findAll();
        return R.ok(list);
    }

    //根据id查询单个Role
    //localhost:8080/role/{id}  ---Get请求方式
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个角色")
    @PreAuthorize(name = "查询单个角色", sn = "role:get", descs = "查询单个角色")
    public R<Role> findOne(@PathVariable("id") Long id) {
        Role role = roleService.findOne(id);
        return R.ok(role);
    }

    //新增 或者 修改
    //前端：Put：http://localhost:8080/role/  json对象 - 请求体
    //@RequestBody Role sysUse 使用Role对象去接受请求体中的数据，要求属性名一致
    @PutMapping
    @ApiOperation(value = "新增or修改角色")
    @PreAuthorize(name = "新增or修改角色", sn = "role:addOrUpdate", descs = "新增or修改角色")
    public R<Void> addOrUpdate(@RequestBody Role role) {
        try {
            //获取请求参数的id
            Long id = role.getId();
            if (id == null) {//新增没有传递id
                roleService.addRole(role);
            } else {
                roleService.updateRole(role);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("网络异常，请稍后重试!");
        }
        return R.ok();
    }

    //删除
    //前端：Delete：http://localhost:8080/role/2
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个角色")
    @PreAuthorize(name = "删除单个角色", sn = "role:deleteById", descs = "删除单个角色")
    public R<Void> deleteById(@PathVariable("id") Long id) {
        try {
            roleService.deteleById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("网络异常，请稍后重试!");
        }
        return R.ok();
    }

    //分页接口+高级查询(根据条件查询后也需要分页展示)
    @PostMapping
    @ApiOperation(value = "高级查询&分页")
    @PreAuthorize(name = "高级查询&分页", sn = "role:queryPage", descs = "高级查询&分页")
    public PageInfo<Role> queryPage(@RequestBody RoleQuery roleQuery) {
        List<Role> roleList = roleService.queryPage(roleQuery);
        //返回的是封装好的分页(总数,list)
        return new PageInfo<Role>(roleList);
    }

    //批量删除，接收前端id数组
    @PatchMapping
    @ApiOperation(value = "批量删除")
    @PreAuthorize(name = "批量删除", sn = "role:deleteRoleByIds", descs = "批量删除")
    public R<Void> deleteRoleByIds(@RequestBody Long[] ids) {
        try {
            roleService.deleteRoleByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            //返回的json格式  {"success":false,"message":"错误信息"}
            return R.fail("网络异常，请稍后重试!");
        }
        return R.ok();
    }
}